var PATH_PREFIX="";

function setPathPrefix(path){
	PATH_PREFIX = path;
}

$(function(){
	WASP_WIDGET.triggerProductInfoSelect("prdcode",$("#prjcode").val());
	 $("#prdcode").on("change", function(){
		$(this).valid && $(this).valid();
	 });
	WASP_WIDGET.triggerSelectOnDicts($("#accCategory"),{dctRootType:"ACC_CATEGORY_TYPE",dctFathType:"ACC_CATEGORY_TYPE"});
	 $("#accCategory").on("change", function(){
		$(this).valid && $(this).valid();
	 });
	 WASP_WIDGET.triggerAccountTypeListSelect("#accTypeId");
	 $("#accTypeId").on("change", function(){
		$(this).valid && $(this).valid();
	 });
	 WASP_WIDGET.triggerInstSelect("#accBankName");
	 $("#accBankName").on("change", function(){
		$(this).valid && $(this).valid();
	 });
	 $('#accLevel').select2({
		placeholder : '账户层级'
	});
	$("#accLevel").on("change", function(){
		$(this).valid && $(this).valid();
	});
	WASP_WIDGET.triggerIRadio("input[type='radio'][name='accClass']");
	bindRadioClick();
	initValidate();
});

//产品层级切换
function accLevelOnChange(){
	if("1" == $("#accLevel").val()){//系列产品
		$("#prdcode").rules("remove");
		$("#prdcode").attr("disabled","disabled");
		$("#spanShow").hide();
		$("#prdcode").val("").trigger("change");//清空产品名称
	}else if("0" == $("#accLevel").val()){//产品
		$("#prdcode").removeAttr("disabled");
		$("#spanShow").show();
		$("#prdcode").rules("add", {
			required:true ,messages:{required:"请选择产品名称"}
		});
	}

	//判断是"已有账户类别"
	if("0" == $("[name='accClass']:checked").val()){
		$("#accBankName").val("");
		WASP_WIDGET.triggerAccountInfoListSelect("#accBankName",$("#accLevel").val());
	}
}

function accCategoryOnChange(){
	if("" != $("#accCategory").val()){
		$("#accTypeId").val("");
		WASP_WIDGET.triggerAccountTypeListSelect("#accTypeId",$("#accCategory").val());
	}
}

function bindRadioClick(){
	$("[name='accClass']").on('ifChecked', function() {
		var accClass = $(this).val();
		if(accClass=='1'){
			$("[name='addAccountFlag']").show();
			$("#accLevel").rules("remove");
			$("#prdcode").rules("remove");
			$("#accBankName").rules("remove");
			$("#accLevel").val("").trigger("change");//账户层级
			$("#prdcode").val("").trigger("change");//产品名称
			$("#accBankName").val("").trigger("change");//开户行
			
			$("#accCategory").rules("add", {
				required:true ,messages:{required:"请选择账户分类"}
			});
			$("#prdcode").rules("add", {
				required:true ,messages:{required:"请选择开户行"}
			});
			$("#accTypeId").rules("add", {
				required:true ,messages:{required:"请输入银行户名"}
			});
			$("#accCode").rules("add", {
				required:true ,messages:{required:"请输入银行账号"}
			});
			$("#accLevel").rules("add", {
				required:true ,messages:{required:"请输入账户层级"}
			});
			$("#prdcode").rules("add", {
				required:true ,messages:{required:"请输入产品名称"}
			});
			$("#accName").rules("add", {
				required:true ,messages:{required:"请输入银行户名"}
			});
			$("#accBankName").rules("add", {
				required:true ,messages:{required:"请输入开户行"}
			});
			 WASP_WIDGET.triggerInstSelect("#accBankName");
			 $("#accBankName").on("change", function(){
				$(this).valid && $(this).valid();
			 });
		}else{
			$("#accLevel").rules("remove");
			$("#prdcode").rules("remove");
			$("#accBankName").rules("remove");
			$("#accCategory").rules("remove");
			$("#accTypeId").rules("remove");
			$("#accName").rules("remove");
			$("#accCode").rules("remove");
			
			$("#accLevel").val("").trigger("change");//账户层级
			$("#prdcode").val("").trigger("change");//产品名称
			$("#accCategory").val("").trigger("change");//账户分类
			$("#accTypeId").val("").trigger("change");//账户类型
			$("#accBankName").val("").trigger("change");//开户行
			$("#accName").val("");//银行户名
			$("#accCode").val("");//银行账号
			$("[name='addAccountFlag']").hide();
			WASP_WIDGET.triggerAccountInfoListSelect("#accBankName","" == $("#accLevel").val()?"1":$("#accLevel").val());
			$("#accLevel").rules("add", {
				required:true ,messages:{required:"请输入账户层级"}
			});
			$("#prdcode").rules("add", {
				required:true ,messages:{required:"请输入产品名称"}
			});
			$("#accBankName").rules("add", {
				required:true ,messages:{required:"请输入开户行"}
			});
		}
		
	});
}

//表单验证
function initValidate(){
	 $("#prdAccountListForm").validate({
	        focusCleanup: true,
	        rules: {
	        	accLevel:{
	        		required: true
	        	},
	        	accTypeId:{
	        		required: true	
	        	},
	        	accCategory:{
	            	required: true	
	            },
	            accBankName:{
	            	required: true
	            },	
	            accName : {
	            	required: true
				},
				accCode : {
					required: true
				}
	        },
	        messages: {
	        	accLevel:{required:"请选择账户层级"},
	        	accTypeId:{required:"请选择账户类型"},
	        	accCategory:{required:"请选择账户分类"},
	        	accBankName :{required:"请选择开户行"},
	        	accName : {required:"请输入银行户名"},
	        	accCode : {required:"请输入银行账号"}
	        }
	    });
}

function closeLayer(){
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
	parent.layer.close(index); //再执行关闭
}

function save(){
	if(!$("#prdAccountListForm").valid()){
		toastr.warning('', '请完善产品新增信息!');
		return false;
	}
	var flag = false;
	//判断账户类别是否为“已有账户”
	if("0" == $("[name='accClass']:checked").val()){
		$("#accId").val($("#accBankName").val());
		$("#accTypeId").rules("remove");
		$("#accCategory").rules("remove");
		$("#accName").rules("remove");
		$("#accCode").rules("remove");
		$.ajax({
			type: 'POST',
			url: PATH_PREFIX + "/checkAddAccountList.do",
			dataType:'json',
			cache: false,
			async: false,
			data:{
				"accLevel" : $("#accLevel").val(),
				"accId":$("#accId").val(),
				"code":"0"==$("#accLevel").val()?$("#prdcode").val():$("#prjcode").val()
			},
			success: function(data){
				if(data.success){
					flag = true;
				}else{
					ctools.alert_sweet(data.message, "error", "" , function(){});
				}
			},
			error:function(xhr){
				switch(xhr.status){
					case 403:sweetAlert("对不起，您无此权限！","","error");break;
					case 404:sweetAlert("对不起，无此页面！", "","error");break;
					case 500:sweetAlert("内部错误，请联系管理员！","","error");break;  
					case 504:sweetAlert("超时，请联系管理员！", "","error");break;  
					case 417:sweetAlert("内部错误，请联系管理员！", "","error");break;  
				}
			}
		});
	}else{
		$("#accId").attr("disabled",true);
		$("#accBankNameNM").val($("#accBankName").text());
		SubmitAndPreventSecond("prdAccountListForm",function(){});
	}
	
	if(flag){
		debugger;
		$("#accId").removeAttr("disabled");
		SubmitAndPreventSecond("prdAccountListForm",function(){});
	}
}